<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="15">
        <!-- 搜索框 -->
        <el-col :span="8">
          <el-input
            v-model="queryInfo.query"
            placeholder="请输入内容"
            clearable
            @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <!-- 添加按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="goAddGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- table 表格区域 -->
      <el-table :data="goodsList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index" label="ID"></el-table-column>
        <!-- 内容列 -->
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" width="75px" label="单价(元)"></el-table-column>
        <el-table-column prop="goods_weight" width="75px" label="重量"></el-table-column>
        <!-- 时间列 -->
        <el-table-column width="180px" label="创建时间">
          <template #default="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column width="180px" label="操作">
          <template #default="scope">
            <!-- 添加操作 -->
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <!-- 删除操作 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoods(scope.row.goods_id)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'List',
  data() {
    return {
      // 商品列表的查询参数对象
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      // 商品列表
      goodsList: [],
      // 总数据条数
      total: 0
    }
  },
  created() {
    // 获取商品列表数据
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      // 发起请求
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      // 失败提示
      if (res.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: '获取商品列表失败!',
          center: true
        })
      }
      // 成功
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 监听每页显示条数变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听当前页码变化
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    async removeGoods(gid) {
      // 确认删除提示
      const confirmResult = await this.$confirm('确认删除该商品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 取消删除提示
      if (confirmResult !== 'confirm') {
        return this.$message({
          type: 'info',
          message: '已取消删除',
          center: true
        })
      }
      // 发起删除请求
      const { data: res } = await this.$http.delete('goods/' + gid)
      // 失败提示
      if (res.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: '删除成功!',
          center: true
        })
      }
      // 成功提示
      this.$message({
        type: 'success',
        message: '删除成功!',
        center: true
      })
      // 刷新列表
      this.getGoodsList()
    },
    // 跳转到添加页面
    goAddGoods() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped></style>
